let animation = wx.createAnimation({
  duration: 4000,//转盘转动时间
  timingFunction: 'ease-out'//动画效果
})
const app = getApp();
Page({
  data: {
    rotate: 0, //度数
    turning: false, //是否点击,
    zhunsj: '',//另一个页面带来的数据
    prize: '',//中奖概率
    count: '',//剩余次数
    prizeList: [
      2100,//一等奖 ...
      2400,//二等奖  ...
      2330,//三等奖...
      2610,//参与奖 ...
      2200,//谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '',//翻滚的字幕
    flag: true,//设置节流
    modalHidden: true,//新的图片模态框
    erdeng: true,
    sandeng: true,
    sideng: true,
    xiexie: true,//新的图片模态框   

    //获取时间
    time_a: '',//当前时间
    CON_VALUE: '',//开始时间
    CON_DESC: '',//结束时间
    dateTime: "",
    dtim: "",
    ajaj: '',//获取中奖奖项
    // Getphone: false,//判断获取手机号
  },

  //判断是否授权
  // authorization() {
  //   if (app.globalData.grant) {
  //     this.setData({
  //       Getphone: true
  //     });
  //   } else {
  //     wx.showModal({
  //       title: '温馨提示',
  //       content: '请先绑定会员信息',
  //       success: function (res) {
  //         if (res.confirm) {//这里是点击了确定以后
  //           wx.navigateTo({
  //             url: '../../pages/member/member',
  //           })
  //         } else {//这里是点击了取消以后
  //           console.log('用户点击取消')
  //         }
  //       }
  //     })
  //   }

  // },


  // 15829715986

  //自定义模态框的取消
  modalCancel: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(222);

  },

  //自定义模态框的确定
  queding: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      showView: true,
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })

    console.log(111);
  },


  // time_a: '',//当前时间
  // CON_VALUE:'',//开始时间
  // CON_DESC:''//结束时间

  //事件点击处理函数
  start: function () {
    // if (this.data.time_a <= this.data.CON_VALUE || this.data.time_a >= this.data.CON_DESC) {

    //   let data = this.data.CON_VALUE
    //   let tim = this.data.CON_DESC
    //   //接口返回的毫秒数如果为string，需要转化为int
    //   let dateTime = getMyDate(parseInt(data));
    //   let dtim = getMyDate(parseInt(tim))


    //   //输出 dateTime：2018-09-20 20:00:00
    //   //方法体
    //   function getMyDate(str) {
    //     let oDate = new Date(str),
    //       oYear = oDate.getFullYear(),
    //       oMonth = oDate.getMonth() + 1,
    //       oDay = oDate.getDate(),
    //       oTime = oYear + '-' + addZero(oMonth) + '-' + addZero(oDay) + ' ';
    //     return oTime;
    //   }
    //   //补零操作
    //   function addZero(num) {
    //     if (parseInt(num) < 10) {
    //       num = '0' + num;
    //     }
    //     return num;
    //   }


    //   this.setData({
    //     dateTime: dateTime,
    //     dtim: dtim
    //   })

    //   wx.showModal({
    //     title: '活动开始时间',
    //     // content: `${this.data.CON_VALUE}- ${this.data.CON_VALUE }`,
    //     content: `${this.data.dateTime}至${this.data.dtim}`,
    //     success: function (res) {
    //       if (res.confirm) {//这里是点击了确定以后
    //         console.log('用户点击确定')
    //       } else {//这里是点击了取消以后
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    // } else {

      // 节流
      if (this.data.flag) {
        this.data.flag = true
        var numr = this.data.count
        if (numr > 0) {
          this.data.flag = false
          setTimeout(() => {
            console.log(numr);
            numr--;
            this.setData({
              count: numr
            })
          }, 4000)
          wx.request({
            url: app.globalData.interface+'/shangLuoWork/API_4005_UserPrizeDraw  ',
            method: 'POST',
            data: {
              USER_ASSETS_WORK_PHONE: this.data.zhunsj.USER_ASSETS_WORK_PHONE,
              KEY:'xianjgjKey9999',
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            // 611000121212521451
            success: res => {
              console.log(res);
              // console.log(res.data.userPrizeDataAfter);
              // console.log(res.data.userPrize[0].USER_PRIZE_ID);
              this.setData({
                // ajaj:res.data.userPrizeDataAfter,
                prize: res.data.prize,
                USER_PRIZE_ID:res.data.userPrize.USER_PRIZE_ID
              })
              setTimeout(() => {
                this.setData({
                  ajaj: res.data.userPrizeDataAfter,
                })
              }, 5000)
              console.log(this.data.ajaj);
              // console.log(this.data.prize);
              // console.log(this.data.USER_PRIZE_ID);
              const index_ = this.data.prize;
              this.rotate(index_)
            }
          })
        } else {
          numr = 0;
          wx.showModal({
            title: '温馨提示',
            content: '您的机会已用完欢迎下次抽奖',
            success: function (res) {
              if (res.confirm) {//这里是点击了确定以后
                console.log('用户点击确定')
              } else {//这里是点击了取消以后
                console.log('用户点击取消')
              }
            }
          })
        }
        setTimeout(() => {
          this.data.flag = true
        }, 5500)
      }
    // }
  },

  // 转盘代码
  rotate: function (index_) {
    // rotate: function () {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1];//直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }

        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          this.setData({
            xiexie: false
          })
        } else if (rdm == 2100) {
          // showModal('一等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2330) {
          // showModal('三等奖');
          this.setData({
            sandeng: false
          })
        } else if (rdm == 2610) {
          // showModal('参与奖');
          this.setData({
            sideng: false
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          this.setData({
            erdeng: false
          })
        }
      }, 5500)
    }
  },


  
  // receive_a() {
  //   this.setData({
  //     showView: true,
  //   })
  // },

  quno() {
    this.setData({
      showView: false,
      login_id: '',
    })
  },

  tjok() {
    console.log('++++++++++++++',1);
    if (this.data.flag) {
      this.data.flag  = true
      console.log(111);
      if (this.data.login_id == '') {
        wx.showToast({
          title: '请填写支付宝账号',
          icon: 'none',
          duration: 2000
        })
      } else {
        this.data.flag = false
        setTimeout(() => {
          console.log('-----------------',1);
          this.data.flag = true
        }, 5000)
      
        console.log(this.data.USER_COUPON_ID);
        console.log(this.data.login_id);
        console.log(this.data.openid);

        wx.request({
          url: app.globalData.interface+'/shangLuoWork/API_1013_sendCouponStateALiPay',
          method: 'POST',
          data: {
            // USER_COUPON_PHONE: this.data.USER_COUPON_PHONE,
            // 变量
            USER_ASSETS_WORK_PHONE:this.data.zhunsj.USER_ASSETS_WORK_PHONE,
            openid:this.data.openid,
            USER_PRIZE_ID : this.data.USER_PRIZE_ID,
            login_id: this.data.login_id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res.data.userCoupon);
            console.log(res.data.result);
            this.setData({
              // NEW_VAILD:res.data.NEW_VAILD,  
              VAILD: res.data.NEW_VAILD,
            })
            console.log(this.data.NEW_VAILD);
            if (res.data.result != 'success') {
              wx.showToast({
                // title: '领取失败',
                title: res.data.data,
                icon: 'none',
                duration: 1800
              })
              this.setData({
                showView: false,
                login_id: '',
              })

            } else {
              wx.showModal({
                title: '温馨提示',
                // content: '领取成功请前往支付宝卡包查看',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) {//这里是点击了确定以后
                    console.log('用户点击确定')
                  } else {//这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
              this.setData({
                showView: false,
                login_id: '',
              })
            }
          }
        })
        const pages = getCurrentPages()
        const perpage = pages[pages.length - 2]
        perpage.onShow()
        this.onShow()
      }
    }
  },

 

  onLoad: function (e) {
    //从上个页面带来的数据
    console.log(e)
    var zhunsj = JSON.parse(e.data)
    // console.log('----', zhunsj.SURPLUS_NUMBER);
    console.log('----', zhunsj);

   this.setData({
        zhunsj: zhunsj,
        // count: zhunsj.byIdCard.SURPLUS_NUMBER,
        // ajaj: zhunsj.userPrize
        count: zhunsj.SURPLUS_NUMBER
      })

    // if (zhunsj.userPrize == false) {
    //   console.log(2222);
    //   this.setData({
    //     zhunsj: zhunsj,
    //     count: zhunsj.byIdCard.SURPLUS_NUMBER,
    //     // ajaj: zhunsj.userPrize
    //     // count:count
    //   })
    // } else {
    //   console.log(1111);
    //   this.setData({
    //     zhunsj: zhunsj,
    //     count: zhunsj.byIdCard.SURPLUS_NUMBER,
    //     ajaj: zhunsj.userPrize
    //     // count:count
    //   })

    // }


    // this.setData({
    //   zhunsj: zhunsj,
    //   count: zhunsj.byIdCard.SURPLUS_NUMBER,
    //   ajaj: zhunsj.userPrize
    //   // count:count
    // })
    // console.log(this.data.zhunsj);
    // console.log(this.data.count);

    //获奖弹窗
    // this.advertising()


    //获取时间
    wx.request({
      url: app.globalData.interface+'/shanyouhui/API_1009_getConfigNote?code=ShangLuoPrizeDate',
      method: 'GET',
      // data: {
      //   code: 'ShangLuoPrizeDate   '
      // },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.config.CON_VALUE);
        this.setData({
          CON_VALUE: res.data.config.CON_VALUE.CON_VALUE,//开始时间
          CON_DESC: res.data.config.CON_VALUE.CON_DESC//结束时间
        })
        console.log(this.data.CON_DESC);
        console.log(this.data.CON_VALUE);
      }
    })

    // 获取时间
    var time_a = new Date().getTime();
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time_a: time_a
    });
    console.log(this.data.time_a);
  },


  onShow() {
    this.setData({
      openid: app.globalData.openid
    });
  },


})